/*
 * Copyright 2014 Hieu Rocker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rockerhieu.emojicon;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

import com.rockerhieu.emojicon.emoji.SerializeManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hieu Rocker (rockerhieu@gmail.com).
 */
public class EmojiconEditText1 extends EditText {
    private int mEmojiconSize;
    private int mEmojiconTextSize;
    private boolean mUseSystemDefault = false;
    private Integer topicNum=0;

    public Integer getTopicNum() {
		return topicNum;
	}

	public void setTopicNum(Integer topicNum) {
		this.topicNum = topicNum;
	}

	public EmojiconEditText1(Context context) {
        super(context);
        mEmojiconSize = (int) getTextSize();
        mEmojiconTextSize = (int) getTextSize();
    }

    public EmojiconEditText1(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public EmojiconEditText1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.Emojicon);
        mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize, getTextSize());
        mUseSystemDefault = a.getBoolean(R.styleable.Emojicon_emojiconUseSystemDefault, false);
        a.recycle();
        mEmojiconTextSize = (int) getTextSize();
        setText(getText());
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        updateText();
        setText1(getText() + "");
    }

    /**
     * Set the size of emojicon in pixels.
     */
    public void setEmojiconSize(int pixels) {
        mEmojiconSize = pixels;

        updateText();
    }  

    private void updateText() {
        EmojiconHandler.addEmojis(getContext(), getText(), mEmojiconSize, mEmojiconTextSize, mUseSystemDefault);
      
    }

    /**
     * Set whether to use system default emojicon
     */
    public void setUseSystemDefault(boolean useSystemDefault) {
        mUseSystemDefault = useSystemDefault;
    }
    
    public void setText1(String html) {
		CharSequence sequence = Html.fromHtml(html);
		List<Integer>mList=getPostation(html,"#");
		NoLineClickSpan[] oldSpans = getText().getSpans(0, sequence.length(),
				NoLineClickSpan.class);
		for (int i = 0; i < oldSpans.length; i++) {
			getText().removeSpan(oldSpans[i]);
		}
		if(null!=mList&&mList.size()>1){
			topicNum=mList.size()/2;
			for(int i=0;i<topicNum;i++){
				String sub=html.substring(mList.get(i*2), mList.get(i*2+1)+1);
				URLSpan clickSpan = new NoLineClickSpan(sub); //���ó�����
//				try {
//					if(i==topicNum-1&&i!=0)
//						getText().setSpan(clickSpan, mList.get(i*2)-1, mList.get(i*2+1), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//					else
//						getText().setSpan(clickSpan, mList.get(i*2), mList.get(i*2+1)+1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} 
				getText().setSpan(clickSpan, mList.get(i*2), mList.get(i*2+1)+1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
			}
		}
		
		String content = html;
		if (content.equals("")) {
		} else {

//		SpannableString ss = new SpannableString(content);
		List<String> tempL = SerializeManager.getUrlsInContent(content);
		if(tempL != null){
		for(String temp : tempL){
		int index = content.indexOf(temp);
		getText().setSpan(new NoLineClickSpan(temp), index, index+temp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		}
		}
	}
    private class NoLineClickSpan extends URLSpan {
 		String text;
 		public NoLineClickSpan(String text) {
 		    super(text);
 		    this.text = text;
 		}

 		@Override
 		public void updateDrawState(TextPaint ds) {
 		    ds.setColor(Color.parseColor("#f55857"));
 		    ds.setUnderlineText(false); //ȥ���»���
 		}

 		@Override
 		public void onClick(View widget) {
 		    //���������ʱ����
 		}
 	}
     private List<Integer> getPostation(String content, String str) {
 		List<Integer> mList = new ArrayList<Integer>();
 		if (null != content && null != str) {
 			int len = content.length();
 			for (int i = 0; i < len; i++) {
 				if (str.equals(String.valueOf(content.charAt(i)))) {
 					mList.add(i);
 				}
 			}
 		}
 		return mList;
 	}
     
}
